package com.haidechizi.fastadmin.modules.sys.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.haidechizi.fastadmin.modules.sys.dao.SysRoleOfficeDao;
import com.haidechizi.fastadmin.modules.sys.entity.SysRoleOffice;
import com.haidechizi.fastadmin.modules.sys.service.SysRoleOfficeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 角色与部门对应关系
 */
@Service
public class SysRoleOfficeServiceImpl extends ServiceImpl<SysRoleOfficeDao, SysRoleOffice> implements SysRoleOfficeService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(Long roleId, List<Long> officeIdList) {
        //先删除角色与部门关系
        deleteBatch(new Long[]{roleId});

        if (officeIdList.size() == 0) {
            return;
        }

        //保存角色与菜单关系
        List<SysRoleOffice> list = new ArrayList<>(officeIdList.size());
        for (Long officeId : officeIdList) {
            SysRoleOffice sysRoleOffice = new SysRoleOffice();
            sysRoleOffice.setOfficeId(officeId);
            sysRoleOffice.setRoleId(roleId);

            list.add(sysRoleOffice);
        }
        this.insertBatch(list);
    }

    @Override
    public List<Long> queryOfficeIdList(Long[] roleIds) {
        return baseMapper.queryOfficeIdList(roleIds);
    }

    @Override
    public int deleteBatch(Long[] roleIds) {
        return baseMapper.deleteBatch(roleIds);
    }
}
